package com.young.android.view;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.young.android.R;
import com.young.android.business.ItemBusi;
import com.young.android.common.BaseActivity;
import com.young.android.common.Const;
import com.young.android.entity.Item;

public class RSSFeedRead extends BaseActivity {

    private String rssUrl;
    private String rssName;

    private static ArrayList<Item> feed;

    TextView titleText;
    ListView listView;

    @Override
    protected void init() {
        // TODO Auto-generated method stub
        setContentView(R.layout.feed_list);

        Intent it = getIntent();

        rssUrl = (String) it.getCharSequenceExtra("url");
        rssName = (String) it.getCharSequenceExtra("name");

        titleText = (TextView) findViewById(R.id.title_text);
        titleText.setText(rssName);

        listView = (ListView) findViewById(R.id.content_list);
        listView.setOnItemClickListener(listClick);
        showList();
        
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();

        // 重启该Activity时刷新ListView
        listView = (ListView) findViewById(R.id.content_list);
        showList();
    }

    // 显示结果到ListView
    public void showList() {
        feed=ItemBusi.getItems(Const.countPerTime, rssUrl, 0);
        ItemListAdapter adapter = new ItemListAdapter(this,feed);
        listView.setAdapter(adapter);
    }

    // 供其他activity获取所读取的RSS
    public static ArrayList<Item> getFeed() {
        if (feed != null) {
            return feed;
        }
        return null;
    }

    private OnItemClickListener listClick=new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

            ItemBusi.setItemState(Const.ITEM_READ,feed.get(position));

            Intent it = new Intent(RSSFeedRead.this, ContentActivity.class);
            Bundle bundle = new Bundle();
            bundle.putInt("position", position);
            bundle.putString("rssName", rssName);

            it.putExtras(bundle);

            startActivity(it);

        }
        
    };

}